package com.bitjiuyeke;

import utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class Demo06_Delete {
    public static void main(String[] args) {
        Connection connection = null;
        PreparedStatement statement = null;
        Scanner scanner = new Scanner(System.in);
        try{
            // 1. 连接数据库
            connection = DBUtil.getConnection();
            //2.定义sql语句
            String sql="delete from student where name=?";
            //3.创建PreparedStatement对象
            statement = connection.prepareStatement(sql);
            //4输入要删除的学生姓名
            System.out.println("请输入要删除的学生姓名：");
            String name = scanner.next();
            //5.设置参数
            statement.setString(1, name);
            //6.执行sql语句
            int  row = statement.executeUpdate();
            if(row>0){
                System.out.println("删除成功！");
            }else{
                System.out.println("删除失败！");
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            //7.关闭资源
            DBUtil.close(null, statement, connection);
            scanner.close();
        }
    }
}
